<?php
$this->widget('bootstrap.widgets.TbBreadcrumbs', array(
    'links'=>array(
        'Admin'=>Yii::app()->baseUrl.'/sysadmin.html',
        $this->model_name
     ),    
));
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').slideToggle('fast');
    return false;
});
$('.search-form form').submit(function(){
    $.fn.yiiGridView.update('loaihinh-grid', {
        data: $(this).serialize()
    });
    return false;
});
");

?>

<?php 
$this->beginWidget('zii.widgets.CPortlet', array(
	'htmlOptions'=>array(
		'class'=>''
	)
));
$this->widget('bootstrap.widgets.TbMenu', array(
	'type'=>'pills',
	'items'=>array(
		array('label'=>'Thêm mới', 'icon'=>'icon-plus', 'url'=>Yii::app()->controller->createUrl('create'), 'linkOptions'=>array()),
                array('label'=>'Danh sách', 'icon'=>'icon-th-list', 'url'=>Yii::app()->controller->createUrl('index'),'active'=>true, 'linkOptions'=>array()),
		array('label'=>'Tìm kiếm', 'icon'=>'icon-search', 'url'=>'#', 'linkOptions'=>array('class'=>'search-button')),
		array('label'=>'Xuất PDF', 'icon'=>'icon-download', 'url'=>Yii::app()->controller->createUrl('GeneratePdf'), 'linkOptions'=>array('target'=>'_blank'), 'visible'=>true),
		array('label'=>'Xuất Excel', 'icon'=>'icon-download', 'url'=>Yii::app()->controller->createUrl('GenerateExcel'), 'linkOptions'=>array('target'=>'_blank'), 'visible'=>true),
                array('label'=>'Xóa', 'icon'=>'icon-remove', 'url'=>'#', 'linkOptions'=>array('id'=>'btndelete'), 'visible'=>true),
                array('label'=>'Active', 'icon'=>'icon-ok', 'url'=>'#', 'linkOptions'=>array('class'=>'btnpublished', 'pl' => 1), 'visible'=>true),
                array('label'=>'Inactive', 'icon'=>'icon-lock', 'url'=>'#', 'linkOptions'=>array('class'=>'btnpublished','pl' => 0), 'visible'=>true),
	),
));
$this->endWidget();
?>



<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->


<?php $this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'loaihinh-grid',
	'dataProvider'=>$model->search(),
        'type'=>'striped bordered condensed',
        'template'=>'{summary}{pager}{items}{pager}',
        'selectableRows' => 2,
	'columns'=>array(
        array(
                    'class' => 'CCheckBoxColumn',
                    'id' => 'cid', 
                    'value' => '$data->id', 
                    'htmlOptions' => array('style' => 'width:20px;')
                ),
		'id',
		'name',
		'slug',
       array(
                    'class'=>'ajaxButton',
                    'header' => 'Kích hoạt',
                    'template'=> '{published}',
                    'htmlOptions' => array(
                        'style' => 'width:70px; text-align:center;'
                    ),
                    'buttons'=>array(
                        'published'=>array(
                            'ajax'=>true,
                            'url'=>'Yii::app()->controller->createUrl("loaihinh/published",array("id"=>$data->id,"status" => abs($data->published-1)))',
                            'options' => array(
                               'pl'=>'$data->published',
                               'title' => '',
                                'data-original-title' => '',
                                'rel' => 'tooltip'
                            ),
                        )
                    ),
                ),
       array(
            'class'=>'bootstrap.widgets.TbButtonColumn',
			'template' => '{view} {update} {delete}',
			'buttons' => array(
			      'view' => array(
					'label'=> 'View',
					'options'=>array(
						'class'=>'btn btn-small view'
					)
				),	
                              'update' => array(
					'label'=> 'Update',
					'options'=>array(
						'class'=>'btn btn-small update'
					)
				),
				'delete' => array(
					'label'=> 'Delete',
					'options'=>array(
						'class'=>'btn btn-small delete'
					)
				)
			),
            'htmlOptions'=>array('style'=>'width: 115px'),
           )
	),
)); ?>




<script type="text/javascript">
    $(document).ready(function(){
        
        $('#btndelete').click(function(){
            var ids = $.fn.yiiGridView.getSelection('loaihinh-grid')
            if(ids == ''){
                alert('Không có gì để xóa');
                return false;
            }else if(!confirm('Bạn có chắc là muốn xóa?')) return false;
            var th = this;
            var afterDelete=function(){};
            $.fn.yiiGridView.update('loaihinh-grid', {
                    type:'POST',
                    url:'<?php echo Yii::app()->controller->createUrl('delete'); ?>?id='+ids,
                    success:function(data) {
                            $.fn.yiiGridView.update('loaihinh-grid');
                            afterDelete(th,true,data);
                    },
                    error:function(XHR) {
                            return afterDelete(th,false,XHR);
                    }
            });
            return false;
            
        });
        $('.btnpublished').click(function(){
                var ids = $.fn.yiiGridView.getSelection('loaihinh-grid');
                var status = $(this).attr('pl');
                var th = this;
                var afterDeletes=function(){};
                $.fn.yiiGridView.update('loaihinh-grid', {
                        type:'POST',
                        url:'<?php echo Yii::app()->controller->createUrl('published'); ?>?id='+ids+'&status='+status,
                        success:function(data) {
                                $.fn.yiiGridView.update('loaihinh-grid');
                                afterDeletes(th,true,data);
                        },
                        error:function(XHR) {
                                return afterDeletes(th,false,XHR);
                        }
                });
                return false;

            });

    });
</script>